package com.espirit.eap.manager;

import java.util.Collection;

import javax.annotation.Resource;

import com.googlecode.cswish.annotation.GenericType;
import com.googlecode.cswish.model.ModelInfo;
import com.googlecode.cswish.model.PropertyInfo;
import com.googlecode.cswish.struts.spring.BeanIntrospector;
import com.googlecode.cswish.struts.spring.GenericService;
import com.googlecode.cswish.util.ClassUtil;
import com.googlecode.cswish.util.bean.ModelUtil;

public abstract class SysSettingServiceSupport {
	@Resource
	protected SystemDataService systemDataService;
	
	@Resource
	BeanIntrospector beanIntrospector;
	
	@Resource
	GenericService genericService;

	protected void reSet(@GenericType Object model){
		ModelInfo mi = beanIntrospector.getModelInfo(model.getClass().getName());
		Collection<PropertyInfo> properties = mi.getAllProperties();
		for(PropertyInfo pi : properties){
			if (pi.isInsertable()) continue;
			Object val = null;
			String property = getPropertyName(pi);
			String type = pi.getType();
			if (pi.isSimpleObj()){
				if  (type.equals("java.lang.String")){
					val = getProperty(property);
				} else if (type.equals("java.lang.Integer")){
					val = getIntProperty(property);
				} else if (type.equals("java.lang.Boolean")){
					val = getBoolProperty(property);
				} else {
					val = getProperty(property);
				}
				ModelUtil.setProperty(model, property, val);
			} else {
				val = getProperty(property);
				if (val != null){
					Object obj = genericService.load(ClassUtil.getClass(type), val);
					ModelUtil.setProperty(model, pi.getName(), obj);
				}
			}
			
			
		}
	}
	
	protected void copyDbProperties(@GenericType Object model){
		Object dbModel = genericService.load(model, false);
		ModelInfo mi = beanIntrospector.getModelInfo(model.getClass().getName());
		Collection<PropertyInfo> properties = mi.getAllProperties();
		for(PropertyInfo pi : properties){
			if (!pi.isInsertable()) continue;
			
			String property = pi.getName();
			Object val = ModelUtil.getProperty(dbModel, property);
			ModelUtil.setProperty(model, property, val);
		}
	}
	
	
	public void _update(@GenericType Object model){
		reSet(model);
	}
	
	private String getPropertyName(PropertyInfo pi){
		String name = pi.getName();
		if (!pi.isSimpleObj()){
			name += ".id";
		}
		return name;
	}
	
	public void update(@GenericType Object model){
		ModelInfo mi = beanIntrospector.getModelInfo(model.getClass().getName());
		Collection<PropertyInfo> properties = mi.getAllProperties();
		for(PropertyInfo pi : properties){
			if (!pi.isInsertable()){
				setSystemDataValue(model, getPropertyName(pi));
			}
		}
		initValues();
	}
	
	private String getProperty(String property){
		return systemDataService.getValue(getPrefix() + property);
	}
	
	private void setSystemDataValue(Object model, String property){
		Object value = ModelUtil.getProperty(model, property);
		String key = getPrefix() + property;
		if (value != null){
			systemDataService.setValue(key, value.toString());
		} else {
			if (value instanceof Boolean){
				systemDataService.setValue(key, Boolean.FALSE.toString());
			} else {
				systemDataService.setValue(key, null);
			}
		}
	}
	
	private Integer getIntProperty(String property){
		String value = getProperty(property);
		if (value == null) return null;
		return Integer.valueOf(value);
	}
	
	private Boolean getBoolProperty(String property){
		String value = getProperty(property);
		if (value == null) return false;
		return Boolean.valueOf(value);
	}
	
	public abstract void initValues();
	public abstract String getPrefix();
}
